package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfHouseScoreMapper;
import com.yun.zf.admin.service.ZfHouseScoreService;
import com.yun.zf.common.app.domain.ZfHouseScore;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-06-16
 */
@Service
public class ZfHouseScoreServiceImpl extends ServiceImpl<ZfHouseScoreMapper, ZfHouseScore> implements ZfHouseScoreService {

    @Override
    public int addOrUpdate(ZfHouseScore houseScore) {
        // todo 变动后推送Es
        QueryWrapper<ZfHouseScore> houseScoreQueryWrapper = new QueryWrapper<>();
        houseScoreQueryWrapper.lambda().eq(ZfHouseScore::getBasicId,houseScore.getBasicId()).eq(ZfHouseScore::getCityCode,houseScore.getCityCode())
                .eq(ZfHouseScore::getProvinceCode,houseScore.getProvinceCode());
     ZfHouseScore score =   baseMapper.selectOne(houseScoreQueryWrapper);
     if (score!=null){
         BeanUtil.copyProperties(houseScore,score, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
       return   baseMapper.update(score,houseScoreQueryWrapper);
     }
        return baseMapper.insert(houseScore);
    }

    @Override
    public ZfHouseScore getHouseScore(Long basicId, Long cityCode, Long provCode) {
        QueryWrapper<ZfHouseScore> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ZfHouseScore::getBasicId,basicId).eq(ZfHouseScore::getCityCode,cityCode).eq(ZfHouseScore::getProvinceCode,provCode);
        return baseMapper.selectOne(wrapper);
    }
}
